package com.foreknow.view;

import com.foreknow.dao.BusinessDao;
import com.foreknow.dao.impl.BusinessDaoImpl;
import com.foreknow.model.Business;
import com.foreknow.model.BusinessTable;
import com.foreknow.service.impl.BusinessServiceImpl;

import java.awt.*;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

public class MainWindow extends JFrame {

	private JPanel contentPane;
	private JTable table;
	private JTextField queryField;
	private BusinessTable bTable = new BusinessTable();
	private BusinessServiceImpl bService = new BusinessServiceImpl();
	private BusinessDaoImpl dao = new BusinessDaoImpl();


	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainWindow frame = new MainWindow();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MainWindow() {

		JCheckBox chckbxNewCheckBox = new JCheckBox("New check box");
		getContentPane().add(chckbxNewCheckBox, BorderLayout.CENTER);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 616, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		contentPane.setLayout(null);

		List<Object> list = bService.bTable();
		bTable.setData(list);
		table = new JTable(bTable);
		contentPane.add(table);

		JScrollPane sp = new JScrollPane(table);
		sp.setBounds(36, 22, 530, 166);
		sp.setViewportView(table);
		contentPane.add(sp);

		queryField = new JTextField();
		queryField.setBounds(36, 211, 91, 30);
		contentPane.add(queryField);
		queryField.setColumns(10);
		
		JButton query = new JButton("搜索");
		query.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String bName = queryField.getText();
				List<Object> list = bService.query(bName);
				bTable.setData(list);
			}
		});
		query.setBounds(136, 210, 63, 30);
		contentPane.add(query);
		
		JButton update = new JButton("修改");
		update.setBounds(204, 210, 63, 30);
		contentPane.add(update);
		update.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                Update up = new Update();
                up.setBid(getTableRow().getBusinessId());
                MainWindow.this.dispose();
            }
        });
		
		JButton delete = new JButton("删除");
		delete.setBounds(270, 210, 63, 30);
		contentPane.add(delete);
				delete.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						/*business.getBusinessId();*/
						Business business = new Business();
						bService.delete(getTableRow().getBusinessId());
						try {
							bTable.setData(dao.bQueryAll(business));
						} catch (SQLException ex) {
							ex.printStackTrace();
						}
						refresh();
					}
				});


		JButton add = new JButton("添加");
		add.setBounds(337, 210, 63, 30);
		contentPane.add(add);
		add.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent actionEvent) {
				Add a = new Add();
				MainWindow.this.dispose();
			}
		});
		
	this.setVisible(true);

	}

	/**
	 * 刷新表格
	 */
	public void refresh(){
		table.setModel(bTable);
	}

    /**
     * 获取选中行的数据
     */
	public Business getTableRow(){
        // 获取哪一行被选中了
        int row = table.getSelectedRow();
        // 根据选中的行，到BusinessTableModel中获取对应的对象
        Business b = (Business) bService.bTable().get(row);
        return b;
    }
}
